package com.server.http.handler.impl;

import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.RedeemCode;

/**
 * 礼包码校验
 * @author Administrator
 *
 */
public class Redeem extends HttpHandler{

	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		String err = "";
		long appid = 0;
		String redeem = null;
		try {
			appid = Long.parseLong(request.getParameter("appid"));
			redeem = request.getParameter("redeem");
		}catch (Exception e) {
			e.printStackTrace();
			err = "上传的参数错误!";
		}

		//state 0:礼包码已被使用  1:获取奖励成功  -1:无效的礼包码
		int state = 0;
		if(redeem == null || redeem.equals("")) {
			err = "上传的参数错误!";
		}else if(redeem.length() != 12) {
			err = "上传的参数错误!";
			state = -1;
		}
		
		if(err.equals("")) {
			if(RedeemCode.VerifyCode(redeem, appid + "")){
				state = DataBaseManager.GetInstance().JudgeRedeemCode(redeem);
			}else {
				state = -1;
			}
		}
		SetReturnData(response,state,HttpProtocol.REDEEMCODE,HttpReturnStatus.Successed);
	}
}
